//
//  XLMineInfoViewController.m
//  Mine
//
//  Created by 于鹏 on 2017/8/26.
//  Copyright © 2017年 YIVIEW. All rights reserved.
//

#import "XLMineInfoViewController.h"
#import "XLMineTextCellModel.h"
#import "XLMineBaseCellModel.h"
#import "XLMineHeadImageCellModel.h"
#import "XLDoctorProfileViewController.h"
//#import <OnlineTime_Category/Lothar+OnlineTime.h>
//#import <TZImagePickerController.h>
#import "Lothar+OnlineTime.h"
#import "TZImagePickerController.h"
#import "UIImage+HandleAvatar.h"
#import "XLUtilities.h"
@interface XLMineInfoViewController ()<TZImagePickerControllerDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (nonatomic, strong)NSMutableArray *textModels;
@property (nonatomic, strong)XLMineHeadImageCellModel *headModel;

@end

@implementation XLMineInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"个人信息";
    XLWeakSelf(self);
    
    //section0
    NSString *portraitUrl = UserInfo.RYLogo;
    if ([portraitUrl isEqualToString:@""]) {
        portraitUrl = [XLUtilities defaultUserPortrait:[RCIM sharedRCIM].currentUserInfo];
    }
    self.headModel = [[XLMineHeadImageCellModel alloc]initWithActionBlock:^(XLMineBaseCellModel *model, NSIndexPath *indexPath) {
        [weakself onTouchPortrait];
    }];
    self.headModel.imageUrl = portraitUrl;
    
//    self.headModel.imageIcon = [UIImage imageNamed:@"placeholderImage"];
   
    //section1
    NSArray *section1TitleArr = @[@"姓名",@"性别",@"手机号",@"医院",@"科室"];
//    NSArray *section1DetailTextArr = @[@"李建英",@"男",@"134 1234 5678",@"广东省深圳市第一人民医院",@"心外科"];
//    NSLog(@"%@",UserInfo.doctorDepartment);
     NSArray *section1DetailTextArr = @[UserInfo.userName,UserInfo.gender,UserInfo.phone,UserInfo.hospital,UserInfo.doctorDepartment];
    self.textModels = [NSMutableArray array];
    for (int i = 0; i<section1TitleArr.count; i++) {
        XLMineTextCellModel *model = [[XLMineTextCellModel alloc]initWithTitle:section1TitleArr[i] detailText:section1DetailTextArr[i] actionBlock:^(XLMineBaseCellModel *model, NSIndexPath *indexPath) {
            XLLog(@"%ld",indexPath.row);
        }];
        [self.textModels addObject:model];
    }
    
    //section2
    NSArray *section2Arr = @[@"个人简介", @"在线时间"];
    NSMutableArray *section2 = [NSMutableArray array];
    for (int i = 0; i<section2Arr.count; i++) {
        XLMineBaseCellModel *model = [[XLMineBaseCellModel alloc]initWithTitle:section2Arr[i] actionBlock:^(XLMineBaseCellModel *model, NSIndexPath *indexPath) {
            if (indexPath.row == 0) {
                //个人简介
                XLDoctorProfileViewController *viewController = [[XLDoctorProfileViewController alloc]init];
                [weakself.navigationController pushViewController:viewController animated:YES];
            } else {
                //在线时间
                UIViewController *viewController = [[Lothar shared]onlineTime_aViewController];
                [weakself.navigationController pushViewController:viewController animated:YES];

            }
        }];
        [section2 addObject:model];
    }
    
    [self.xl_dataSource addObject:@[self.headModel]];
    [self.xl_dataSource addObject:self.textModels];
    [self.xl_dataSource addObject:section2];
    [self.tableView reloadData];
}

- (void)initData {
    
}

#pragma mark - 点击头像
- (void)onTouchPortrait {
    UIAlertController *alertController=[UIAlertController alertControllerWithTitle:@"设置头像" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *actionCamera=[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
//        [self takePhoto];
//        self.tabBarController.tabBar.alpha = 0;
        [self showImagePicker:UIImagePickerControllerSourceTypeCamera];
        
    }];
    
    UIAlertAction *actionPhotoLIbrary=[UIAlertAction actionWithTitle:@"从相册中获取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [self pushImagePickerController];
    }];
    
    UIAlertAction *cancelAction=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    [alertController addAction:actionCamera];
    
    [alertController addAction:actionPhotoLIbrary];
    
    [alertController addAction:cancelAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
}

//照相
- (void)showImagePicker:(UIImagePickerControllerSourceType)type {
    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
    picker.delegate = self;
    picker.sourceType = type;
    picker.allowsEditing = YES;
    [self presentViewController:picker animated:YES completion:nil];
}
//跳转相册
- (void)pushImagePickerController {
    
    TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 columnNumber:4 delegate:self];
    
    // 3. 设置是否可以选择视频/图片/原图
    imagePickerVc.allowPickingVideo = NO;
    imagePickerVc.allowPickingImage = YES;
    imagePickerVc.allowPickingOriginalPhoto = YES;
    
    // 4. 照片排列按修改时间升序
    imagePickerVc.sortAscendingByModificationDate = YES;

    XLWeakSelf(self);
    [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        
        if (photos[0] != nil) {
            [weakself uploadImage:photos[0]];
            
        }
    }];
    
    [self presentViewController:imagePickerVc animated:YES completion:nil];
}

#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    UIImage *image = info[UIImagePickerControllerEditedImage];
    [picker dismissViewControllerAnimated:YES completion:^{
        [self uploadImage:image];
    }];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:nil];
}
//更新头像
- (void)uploadImage:(UIImage *)image {

    [self showWithStatus:@"正在修改头像..."];
    UIImage *imageForAcatarUpload = [image imageForAvatarUpload];
  
    [XLHTTPRequest UM_uploadUserLogoWithUserID:UserInfo.userID token:UserInfo.tokenCode image:imageForAcatarUpload progress:^(NSProgress *progress) {
        
    } success:^(id responseObject) {
        [self showSuccessWithStatus:@"头像修改成功!"];
        self.headModel.imageIcon = imageForAcatarUpload;
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
        
        if (self.updateAvatarBlock) {
            self.updateAvatarBlock();
        }
        
    } failure:^(NSError *error, NSString *info) {
        if (error == nil) {
            [self exitLogin:info];
        }
        [self showErrorWithStatus:@"头像修改失败，网络超时!"];
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
